package com.ebelter.myehc.ui.fragments.modifys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.myehc.R;
import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.view.CombinView6;
import com.ebelter.myehc.utils.ViewUtilsEHC;

import org.greenrobot.eventbus.EventBus;


/**
 * 血糖
 */

public class ModifyShHSet_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "ModifyShHSet_Fragment";

    private TextView m_title_tv;
    private TextView m_righttitle_tv;
    private ImageView top_left_iv;


    private CombinView6 shhset_cb6_15m;
    private CombinView6 shhset_cb6_30m;
    private CombinView6 shhset_cb6_60m;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modify_shhset;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void setListener() {
        super.setListener();

        m_righttitle_tv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);

    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        m_title_tv.setText(R.string.shhshzh);
        m_righttitle_tv.setText(R.string.done);


        int shh_xl_time = UserSpUtil.readInt(Constants.IUser.SHH_XL_TIME,10);
        if (shh_xl_time==60){
            setTime(60);
        }else if (shh_xl_time==30){
            setTime(30);
        }else {
            setTime(10);
        }
    }

    private void FV() {
        m_title_tv=mRootView.findViewById(R.id.m_title_tv);
        m_righttitle_tv=mRootView.findViewById(R.id.m_righttitle_tv);
        top_left_iv=mRootView.findViewById(R.id.top_left_iv);

        shhset_cb6_15m=mRootView.findViewById(R.id.shhset_cb6_15m);
        shhset_cb6_30m=mRootView.findViewById(R.id.shhset_cb6_30m);
        shhset_cb6_60m=mRootView.findViewById(R.id.shhset_cb6_60m);

        shhset_cb6_15m.setOnClickListener(this);
        shhset_cb6_30m.setOnClickListener(this);
        shhset_cb6_60m.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.m_righttitle_tv:
                complete ();
                break;
            case R.id.top_left_iv:
                back();
                break;
            case R.id.shhset_cb6_15m:
                setTime(10);
                break;
            case R.id.shhset_cb6_30m:
                setTime(30);
                break;
            case R.id.shhset_cb6_60m:
                setTime(60);
                break;
        }
    }

    private void back() {   if (mActivity!=null)mActivity.finish();
    }

    private void complete() {
        UserSpUtil.writeInt(Constants.IUser.SHH_XL_TIME,minDuration);
        //如果此时手环正在连接 要下发手环心率周期时间
        CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,minDuration,"设置手环周期时间");
        EventBus.getDefault().post(commonEventBus);
        ToastUtil.show(R.string.Modify_Success);
    }


    private int minDuration;
    private void setTime(int min){
        ViewUtilsEHC.setCb6Select(shhset_cb6_15m,false);
        ViewUtilsEHC.setCb6Select(shhset_cb6_30m,false);
        ViewUtilsEHC.setCb6Select(shhset_cb6_60m,false);
        if (min==10){
            ViewUtilsEHC.setCb6Select(shhset_cb6_15m,true);
        }else if (min==30){
            ViewUtilsEHC.setCb6Select(shhset_cb6_30m,true);
        }else if (min==60){
            ViewUtilsEHC.setCb6Select(shhset_cb6_60m,true);
        }
        minDuration=min;
    }



}
